/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/5 15:06
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

(function () {

    "use strict";

    var api = {
        'saveSettingUrl': '/system/wx/setScale',
        'getScaleUrl': '/system/wx/getScale',
        'saveBillSettingUrl': '/system/wx/saveBill',
        'getBillSettingUrl': '/system/wx/getBill'
    };

    sm.controller("admin-wx-setting", ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.scale = {};
        $scope.showBillImageUrl = '';
        $scope.bill = {
            image: '',
            local: {
                x: 0,
                y: 0
            },
            size: {
                width: 0,
                height: 0
            }
        };

        $http.get(api.getScaleUrl).success(function (res) {
            if (res.state) {
                $scope.scale.level_1 = res.data.level_1;
                $scope.scale.level_2 = res.data.level_2;
            } else {

            }
        });

        $scope.save = function () {

            if ($scope.scale.level_1 && $scope.scale.level_1 != NaN) {
                $scope.scale.level_1 = parseFloat($scope.scale.level_1);
            }

            if ($scope.scale.level_2 && $scope.scale.level_2 != NaN) {
                $scope.scale.level_2 = parseFloat($scope.scale.level_2);
            }

            $http.post(api.saveSettingUrl, $scope.scale).success(function (res) {
                if (res.state) {
                    Modal.success('保存成功');
                } else {
                    Modal.error('保存失败');
                }
            });
        };


        // ===========
        // 初始化Web Uploader
        var uploader = WebUploader.create({
            auto: true,
            server: '/system/wx/upload',
            pick: '#billBtn',
            swf: '/assets/library/webuploader/Uploader.swf',
            duplicate: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png'
            }
        });

        // 成功
        uploader.on('uploadSuccess', function(file, res) {
            if (res.state) {
                $scope.bill.image = res.data.key;
                $scope.showBillImageUrl = res.data.fullPath;
            } else {
                var error = res.error ? res.error : '上传失败';
                Modal.error(error);
            }
        });

        uploader.on( 'uploadComplete', function( file ) {
            $scope.$apply();
        });


        $scope.getBill = function () {
            $http.get(api.getBillSettingUrl).success(function (res) {
                if (res.state) {
                    if (res.data.image) {
                        $scope.showBillImageUrl = res.data.image;
                    }
                    $scope.bill = res.data
                }
            });
        };
        $scope.getBill();

        $scope.saveBill = function () {

            if (!$scope.bill.image) {
                Modal.alert('请上传推广二维码背景图片');
                return false;
            }

            if (!$scope.bill.local.x || !$scope.bill.local.y) {
                Modal.alert('请填写二维码起始点坐标');
                return false;
            }

            if (!$scope.bill.size.width || !$scope.bill.size.height) {
                Modal.alert('请填写二维码图片的宽高');
                return false;
            }

            $http.post(api.saveBillSettingUrl, $scope.bill).success(function (res) {
                if (res.state) {
                    Modal.success('设置成功');
                } else {
                    Modal.error('保存失败');
                }
            });

        };

    }]);


})();